using UnityEngine;
using System.Collections;

public class Spriter : MonoBehaviour {
		
	public Texture[] texturesPack;
	private float waitTime = 0.2f;
	
	// Use this for initialization
	void Start () {
		StartTextureAnimation();
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	//Play animation
	public void StartTextureAnimation () {
		StartCoroutine(TextureAnimation());
	}
	
	//Sprite animation
	private IEnumerator TextureAnimation () {
		
		if(texturesPack.Length == 0) {
			yield break;
		}
		
		//Loop forever
		byte textureIndex = 0;
		float rndWaitTime = 0;
		while(true) {
			renderer.material.mainTexture = texturesPack[textureIndex];
			
			rndWaitTime = waitTime + Random.Range(0.05f, 0.5f);
			yield return new WaitForSeconds(rndWaitTime);
			
			if(textureIndex < texturesPack.Length-1) {
				textureIndex++;
			}
			else {
				textureIndex = 0;
			}
		}
	}
}
